using System;
using System.Data;
using System.Data.Common;
using System.Configuration;
using System.Data.SqlClient;

using SIS.ENT;


namespace SIS.DAL
{
    /// <summary>
    /// The SubjectTableDB class is responsible for interacting with the database to retrieve and store information
    /// about SubjectTable objects.
    /// </summary>
    public class SubjectTableDB
    {
        #region "Public Methods"

        /// <summary>
        /// Gets an instance of SubjectTable from the underlying datasource.
        /// </summary>
        /// <param name="subjectKey">The unique SubjectKey of the SubjectTable in the database.</param>
        /// <returns>An SubjectTable when the SubjectKey was found in the database, or null otherwise.</returns>
        public static SubjectTable GetItem(int subjectKey)
        {
            SubjectTable mySubjectTable = null;
            using (SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["sis.dbconn"].ConnectionString))
            {
                using (SqlCommand myCommand = new SqlCommand("sprocSubjectTableSelectSingleItem", myConnection))
                {
                    myCommand.CommandType = CommandType.StoredProcedure;
                    myCommand.Parameters.AddWithValue("@subjectKey", subjectKey);

                    myConnection.Open();
                    using (SqlDataReader myReader = myCommand.ExecuteReader())
                    {
                        if (myReader.Read())
                        {
                            mySubjectTable = FillDataRecord(myReader);
                        }
                        myReader.Close();
                    }
                    myConnection.Close();
                }
                return mySubjectTable;
            }
        }

        /// <summary>
        /// Returns a list with SubjectTable objects.
        /// </summary>
        /// <returns>A generics List with the SubjectTable objects.</returns>
        public static SubjectTableList GetList(int SchoolKey, int SchoolMajorKey, int StandardKey)
        {
            SubjectTableList tempList = new SubjectTableList();
            using (SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["sis.dbconn"].ConnectionString))
            {
                using (SqlCommand myCommand = new SqlCommand("sprocSubjectTableSelectList", myConnection))
                {
                    myCommand.CommandType = CommandType.StoredProcedure;
                    myCommand.Parameters.AddWithValue("@schoolKey", SchoolKey);
                    myCommand.Parameters.AddWithValue("@schoolMajorKey", SchoolMajorKey);
                    myCommand.Parameters.AddWithValue("@standardLevelKey", StandardKey);
                    myConnection.Open();
                    using (SqlDataReader myReader = myCommand.ExecuteReader())
                    {
                        if (myReader.HasRows)
                        {
                            while (myReader.Read())
                            {
                                tempList.Add(FillDataRecord(myReader));
                            }
                        }
                        myReader.Close();
                    }
                }
            }
            return tempList;
        }

        /// <summary>
        /// Saves a SubjectTable in the database.
        /// </summary>
        /// <param name="mySubjectTable">The SubjectTable instance to save.</param>
        /// <returns>The new SubjectKey if the SubjectTable is new in the database or the existing SubjectKey when an item was updated.</returns>
        public static int Save(SubjectTable mySubjectTable)
        {
            int result = 0;
            using (SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["sis.dbconn"].ConnectionString))
            {
                using (SqlCommand myCommand = new SqlCommand("sprocSubjectTableInsertUpdateSingleItem", myConnection))
                {
                    myCommand.CommandType = CommandType.StoredProcedure;

                    if (mySubjectTable.SubjectKey == -1)
                    {
                        myCommand.Parameters.AddWithValue("@subjectKey", DBNull.Value);
                    }
                    else
                    {
                        myCommand.Parameters.AddWithValue("@subjectKey", mySubjectTable.SubjectKey);
                    }

                    if (mySubjectTable.SchoolKey <= 0)
                    {
                        myCommand.Parameters.AddWithValue("@schoolKey", DBNull.Value);
                    }
                    else
                    {
                        myCommand.Parameters.AddWithValue("@schoolKey", mySubjectTable.SchoolKey);
                    }

                    if (mySubjectTable.SchoolMajorKey <= 0)
                    {
                        myCommand.Parameters.AddWithValue("@schoolMajorKey", DBNull.Value);
                    }
                    else
                    {
                        myCommand.Parameters.AddWithValue("@schoolMajorKey", mySubjectTable.SchoolMajorKey);
                    }

                    if (mySubjectTable.StandardLevelKey <= 0)
                    {
                        myCommand.Parameters.AddWithValue("@standardLevelKey", DBNull.Value);
                    }
                    else
                    {
                        myCommand.Parameters.AddWithValue("@standardLevelKey", mySubjectTable.StandardLevelKey);
                    }

                    if (string.IsNullOrEmpty(mySubjectTable.SubjectCode))
                    {
                        myCommand.Parameters.AddWithValue("@subjectCode", DBNull.Value);
                    }
                    else
                    {
                        myCommand.Parameters.AddWithValue("@subjectCode", mySubjectTable.SubjectCode);
                    }
                    if (string.IsNullOrEmpty(mySubjectTable.SubjectName))
                    {
                        myCommand.Parameters.AddWithValue("@subjectName", DBNull.Value);
                    }
                    else
                    {
                        myCommand.Parameters.AddWithValue("@subjectName", mySubjectTable.SubjectName);
                    }
                    if (string.IsNullOrEmpty(mySubjectTable.Description))
                    {
                        myCommand.Parameters.AddWithValue("@description", DBNull.Value);
                    }
                    else
                    {
                        myCommand.Parameters.AddWithValue("@description", mySubjectTable.Description);
                    }
                    if (mySubjectTable.Status == null)
                    {
                        myCommand.Parameters.AddWithValue("@status", DBNull.Value);
                    }
                    else
                    {
                        myCommand.Parameters.AddWithValue("@status", mySubjectTable.Status);
                    }

                    //if (mySubjectTable.CreatedDateTime == null)
                    //{
                    //    myCommand.Parameters.AddWithValue("@createdDateTime", DBNull.Value);
                    //}
                    //else
                    //{
                    //    myCommand.Parameters.AddWithValue("@createdDateTime", mySubjectTable.CreatedDateTime);
                    //}
                    //if (mySubjectTable.ModifiedDateTime == null)
                    //{
                    //    myCommand.Parameters.AddWithValue("@modifiedDateTime", DBNull.Value);
                    //}
                    //else
                    //{
                    //    myCommand.Parameters.AddWithValue("@modifiedDateTime", mySubjectTable.ModifiedDateTime);
                    //}

                    DbParameter returnValue;
                    returnValue = myCommand.CreateParameter();
                    returnValue.Direction = ParameterDirection.ReturnValue;
                    myCommand.Parameters.Add(returnValue);

                    myConnection.Open();
                    myCommand.ExecuteNonQuery();
                    result = Convert.ToInt32(returnValue.Value);
                    myConnection.Close();
                }
            }
            return result;
        }

        /// <summary>
        /// Deletes a SubjectTable from the database.
        /// </summary>
        /// <param name="subjectKey">The SubjectKey of the SubjectTable to delete.</param>
        /// <returns>Returns true when the object was deleted successfully, or false otherwise.</returns>
        public static bool Delete(int subjectKey)
        {
            int result = 0;
            using (SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["sis.dbconn"].ConnectionString))
            {
                using (SqlCommand myCommand = new SqlCommand("sprocSubjectTableDeleteSingleItem", myConnection))
                {
                    myCommand.CommandType = CommandType.StoredProcedure;

                    myCommand.Parameters.AddWithValue("@subjectKey", subjectKey);
                    myConnection.Open();
                    result = myCommand.ExecuteNonQuery();
                    myConnection.Close();
                }
            }
            return result > 0;
        }

        #endregion

        /// <summary>
        /// Initializes a new instance of the SubjectTable class and fills it with the data fom the IDataRecord.
        /// </summary>
        private static SubjectTable FillDataRecord(IDataRecord myDataRecord)
        {
            SubjectTable mySubjectTable = new SubjectTable();
            if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("SubjectKey")))
            {
                mySubjectTable.SubjectKey = myDataRecord.GetInt32(myDataRecord.GetOrdinal("SubjectKey"));
            }
            if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("SchoolKey")))
            {
                mySubjectTable.SchoolKey = myDataRecord.GetInt32(myDataRecord.GetOrdinal("SchoolKey"));
            }
            if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("SchoolMajorKey")))
            {
                mySubjectTable.SchoolMajorKey = myDataRecord.GetInt32(myDataRecord.GetOrdinal("SchoolMajorKey"));
            }
            if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("StandardLevelKey")))
            {
                mySubjectTable.StandardLevelKey = myDataRecord.GetInt32(myDataRecord.GetOrdinal("StandardLevelKey"));
            }

            if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("SubjectCode")))
            {
                mySubjectTable.SubjectCode = myDataRecord.GetString(myDataRecord.GetOrdinal("SubjectCode"));
            }
            if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("SubjectName")))
            {
                mySubjectTable.SubjectName = myDataRecord.GetString(myDataRecord.GetOrdinal("SubjectName"));
            }
            if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("Description")))
            {
                mySubjectTable.Description = myDataRecord.GetString(myDataRecord.GetOrdinal("Description"));
            }
            if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("Status")))
            {
                mySubjectTable.Status = myDataRecord.GetBoolean(myDataRecord.GetOrdinal("Status"));
            }
            if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("CreatedDateTime")))
            {
                mySubjectTable.CreatedDateTime = myDataRecord.GetDateTime(myDataRecord.GetOrdinal("CreatedDateTime"));
            }
            if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("ModifiedDateTime")))
            {
                mySubjectTable.ModifiedDateTime = myDataRecord.GetDateTime(myDataRecord.GetOrdinal("ModifiedDateTime"));
            }
            return mySubjectTable;
        }
    }

}
